package com.example.answer_staff;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.answer_staff.api.ApiManager;
import com.gaosai.staff.commlistadapter.ListAndActivityListAdapter;
import com.gaosai.staff.domain.MessageResult;
import com.gaosai.staff.utils.JsonUtils;
import com.gaosai.staff.xlistview.XListView;
import com.gaosai.staff.xlistview.XListView.IXListViewListener;
import com.loopj.android.http.AsyncHttpResponseHandler;
/**
 * 搜索新闻和活动列表
 * @author willis
 *
 */
public class SearchNowsListActivity extends BaseActivity implements IXListViewListener{

	private ArrayList<String> news_list ;
	private ListAndActivityListAdapter adapter ;
	private XListView listview_news;
	private RelativeLayout searchBarRl ;
	private ImageButton backButton ;
	private EditText keyWords ;
	
	private Button search ;
	
	private int page = 1 ;
	
	private String searchKeywordContent ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentViewItem(R.layout.newsroom_listview) ;
		initView() ;
		setOnClickListener() ;
	}
	
	/**初始化组件**/
	private void initView(){
		
		//隐藏普通的标题栏
		hideCommTitleBar() ;
		//取得搜索标题栏
		searchBarRl = getSearchBar() ;
		searchBarRl.setVisibility(View.VISIBLE) ;

		keyWords = (EditText) searchBarRl.findViewById(R.id.search_keyword) ;
		search = (Button) searchBarRl.findViewById(R.id.search_tv) ;
		backButton = (ImageButton) searchBarRl.findViewById(R.id.backbutton) ;
		
		news_list=new ArrayList<String>();
		
		listview_news=(XListView)findViewById(R.id.listView_newsroom);
		
		listview_news.setPullRefreshEnable(true);
		listview_news.setPullLoadEnable(true);
		listview_news.setXListViewListener(this) ;
		adapter = new ListAndActivityListAdapter(getApplicationContext(), news_list) ;
		listview_news.setAdapter(adapter);
	}

	private void getSearchData() {
		/**
		 * 展示活动
		 * */
		ApiManager.getInstance().showNews(SearchNowsListActivity.this,page,searchKeywordContent,"", new AsyncHttpResponseHandler() {
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String result = new String(arg2) ;
				//				Log.e("NewsRoom——-success", result) ;
				MessageResult messageResult = MessageResult.prase(result) ;
				if(0==messageResult.getCode()){
					news_list.addAll(JsonUtils.JsonResult(messageResult.getData())) ;
					adapter.notifyDataSetChanged() ;
					System.out.println("news_list是"+news_list);
				}else{
					Toast.makeText(SearchNowsListActivity.this, messageResult.getMsg(), 0).show() ;
				}
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
				Log.e("NewsRoom---Faliure", "666666") ;
			}
		}) ;
	}
	private void setOnClickListener(){
		search.setOnClickListener(searchOnClickListener) ;
		backButton.setOnClickListener(backOnClickListener) ;
		
		listview_news.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				String str=news_list.get(position-1);           
				String news_type = getType(str);
				if(news_type.equals("new")){
					Intent intent=new Intent(SearchNowsListActivity.this,ListJournalismDetailsActivity.class);
					intent.putExtra("new", str) ;
					startActivity(intent);

				} else{
					Intent intent=new Intent(SearchNowsListActivity.this,ListCampDetailsActivity.class);
					intent.putExtra("new", str) ;
					startActivity(intent);	
				}
			}

			private String  getType(String str) {

				JSONObject jsonObject;
				String type = null ;
				try {
					jsonObject = new JSONObject(str);
					type = jsonObject.getString("types");
				} catch (JSONException e) {
					e.printStackTrace();
				}
				return type;
			}
		});
	}
	@Override
	public void onRefresh() {
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				page = 1 ;
				news_list.clear() ;
				getSearchData() ;
				
				listview_news.stopLoadMore() ;
				listview_news.stopRefresh() ;
				
			}
		}, 2000) ;
	}

	@Override
	public void onLoadMore() {
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				page++ ;
				getSearchData() ;
				listview_news.stopRefresh() ;
				listview_news.stopLoadMore() ;
			}
		}, 2000) ;
	}
	
	/**返回按钮的监听事件**/
	private View.OnClickListener backOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			finish() ;
		}
	};
	/**搜索按钮的监听事件**/
	private View.OnClickListener searchOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			hideIm() ;
			searchKeywordContent = keyWords.getText().toString().trim() ;
			if(TextUtils.isEmpty(searchKeywordContent)){
				Toast.makeText(SearchNowsListActivity.this, "Search editText can not be empty", 0).show();
				return ;
			}
			getSearchData() ;
		}
		
	};
	
	
	
}
